package com.example.wyj.editdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.telephony.PhoneNumberFormattingTextWatcher;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;

public class MainActivity2 extends AppCompatActivity {

    private EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText = findViewById(R.id.edit_text);
        editText.addTextChangedListener(loginInputAccountWatcher);
    }

    private TextWatcher loginInputAccountWatcher = new PhoneNumberFormattingTextWatcher() {

        /**
         * @param s          变化后的文本
         * @param start      开始变化的索引
         * @param before     原字符的长度
         * @param inputCount start开始输入count个
         */
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int inputCount) { // total 12 start 11 before: 改变原来字符串的个数 input: 0 | 1
            Log.i("COCO", "onTextChanged: CharSequence " + s + "  start" + start + "  before:" + before + "  count:" + inputCount);
            if (s == null || s.length() == 0) {
                return;
            }
            //int startCursor = editText.getSelectionStart();
            int startCursor = start + inputCount;
            String phone = s.toString().replace(" ", "");
            boolean hasFormat = s.toString().contains(" ");

            if (!TextUtils.isEmpty(phone)) {
                if (hasFormat) {
                    //去格式化
                    if (inputCount == 1) {
                        if (3 < startCursor && startCursor <= 9) {
                            startCursor -= 1;
                        } else if (startCursor > 9) {
                            startCursor -= 2;
                        }
                    } else if (before >= 1) { //多个字符剪贴, before > 1
                        if (3 < startCursor && startCursor < 9 && startCursor != 8) {
                            startCursor -= 1;
                        } else if (startCursor >= 9) {
                            startCursor -= 2;
                        }
                    }
                } else if (s.toString().length() == 11) {
                    if (3 < startCursor && startCursor < 8) {
                        startCursor += 1;
                    } else if (startCursor >= 8) {
                        startCursor += 2;
                    }
                }
                if (inputCount == 1) { // 输入字符串
                    if (phone.length() == 11) {
                        StringBuilder builder = new StringBuilder(phone);
                        builder.insert(3, " ");
                        builder.insert(8, " ");
                        phone = builder.toString();

                        editText.setText(phone);
                    } else if (phone.length() > 11) {
                        editText.setText(phone);
                    }
                } else if (inputCount == 0) { // 删除字符串
                    if (phone.length() == 11) {
                        StringBuilder builder = new StringBuilder(phone);
                        builder.insert(3, " ");
                        builder.insert(8, " ");
                        phone = builder.toString();

                    }
                    editText.setText(phone);
                }
                editText.setSelection(startCursor > phone.length()? phone.length() : startCursor);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }


        @Override
        public void afterTextChanged(Editable s) {

        }
    };
}
